<template>
	<view class="page">
		<u-form :model="editForm" :rules="rules" labelWidth="auto" ref="uForm">
			<u-form-item label=" " prop="avatar" borderBottom>
				<view style="display: flex; width: 100%; justify-content: space-between">
					<u-avatar :src="$imageOss(editForm.avatar)" />
					<view class="" @click="upLoadAvatar" style="text-align: right;display: flex;padding: 20rpx 0;">
						<view class="" style="color: #909090;">
							修改头像
						</view>
						<u-icon slot="right" name="arrow-right" />
					</view>
				</view>
			</u-form-item>
			<u-form-item label="昵称" prop="nickname" borderBottom>
				<u-input v-model="editForm.nickname" border="none" placeholder="请输入昵称" :required="true" maxlength="10"
					input-align="right" font-size="28rpx" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item label="手机号" prop="mobile" borderBottom>
				<u-input v-model="editForm.mobile" border="none" placeholder="请输入手机号" :required="true" maxlength="11"
					:disabled="true" disabledColor="#fff" input-align="right" font-size="28rpx"
					:placeholder-style="placeholderStyle" />
			</u-form-item>
		</u-form>

		<view class="btn">
			<u-button type="primary" shape="circle" text="保存" :throttle-time="1000" @click="handleSubmit" />
		</view>
		<u-datetime-picker :show="show" v-model="value1" mode="date" @confirm="confirmbirthday"
			@cancel="show = false"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		commonUpload
	} from "@/request/common.js";
	import {
		getuserinfo,
		userUpdate,
	} from "@/request/myApi.js";
	export default {
		data() {
			return {
				editForm: {
					avatar: '',
					nickname: '',
					mobile: '',
				}, // 编辑表单
				value1: Number(new Date()),
				rules: {
					avatar: {
						type: "string",
						required: true,
						message: "请上传头像",
						trigger: ["blur", "change"],
					},
					nickname: {
						type: "string",
						required: true,
						message: "请填写昵称",
						trigger: ["blur", "change"],
					},
				},
				show: false,
				placeholderStyle: {
					fontSize: "28rpx",
					color: "#D9D9D9",
				},
			};
		},
		onReady() {
			//兼容微信小程序，只能通过setRules方法设置。
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {
			this.getUserInfo()
		},
		onShow() {

		},
		methods: {
			// 获取用户信息
			getUserInfo() {
				let that = this
				getuserinfo().then(res => {
					that.userInfo = res.data
					that.$pageckForIn(that.editForm, that.userInfo)
				})
			},

			// 提交修改
			handleSubmit() {
				this.$refs.uForm.validate().then(() => {
					userUpdate(this.editForm).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: res.msg,
								icon: "none",
							});

							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
							}, 1500);
						} else {
							uni.showToast({
								title: res.msg,
								duration: 1500,
								icon: 'none',
							})
						}
					});
				});
			},
			// 上传头像
			upLoadAvatar() {
				uni.chooseImage({
					count: 1, // 单选
					sizeType: ["original"],
					sourceType: ["album", "camera"], //相册 相机
					success: (res) => {
						commonUpload(res.tempFilePaths[0]).then((res) => {
							if (res.code == 200) {
								this.editForm.avatar = res.data[0];
							} else {
								uni.showToast({
									title: res.msg,
									icon: "none",
								});
							}
						});
					},
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.page {
		padding: 20rpx 38rpx;

		.btn {
			position: fixed;
			bottom: 100rpx;
			left: 30rpx;
			right: 30rpx;
		}

		/deep/ .u-button {
			background: #58B99DCC;
			border-color: transparent;
		}

		/deep/ .u-form-item__body {
			padding: 28rpx 0;
		}

		/deep/ .u-line {
			border-color: #D9D9D999;
		}
	}
</style>